package cate.game.role.show;

import cate.common.game.GameCollection;
import cate.common.table.d.GDBlog;
import cate.common.table.d.GDOperation;
import cate.common.table.show.row.ActiveItemRow;
import cate.common.util.GameResult;
import cate.game.res.MixRes;
import cate.game.res.MixResAdder;
import cate.game.res.RewardShow;
import cate.game.role.Role;
import cate.game.role.RolePart;
import cate.game.role.bag.prop.Prop;
import cate.game.role.show.avatar.RoleAvatar;
import cate.game.role.show.bubble.RoleBubbleBox;
import cate.game.role.show.frame.RoleHeadFrame;
import cate.game.role.show.heroskin.RoleHeroSkin;
import cate.game.role.show.icon.RoleHeadIcon;
import cate.game.role.show.leaderskin.RoleLeaderSkin;
import cate.game.role.show.msg.ActiveDecorationResp;
import cate.game.role.show.msg.RoleShowResp;
import cate.game.role.show.title.RoleTitle;
import cate.game.role.show.vehicle.RoleVehicleSkin;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.mongodb.core.mapping.Document;

@SuppressWarnings("unchecked")
@Document(GameCollection.NAME_ROLE_SHOW)
@NoteClass(value = "主角的展示数据")
public class RoleShow extends RolePart {

	@NoteField("头像")
	public RoleHeadIcon headIcon;

	@NoteField("头像框")
	public RoleHeadFrame headFrame;

	@NoteField("冒险形象")
	public RoleAvatar avartar;

	@NoteField("称号")
	public RoleTitle title;

	@NoteField("英雄皮肤")
	public RoleHeroSkin heroSkin;

	@NoteField("主角皮肤")
	public RoleLeaderSkin leaderSkin;

	@NoteField("气泡框")
	public RoleBubbleBox bubbleBox;

	@NoteField("载具皮肤")
	public RoleVehicleSkin vehicle;


	public RoleShow() {
	}

	public RoleShow(Role role) {
		super(role);
	}


	@Override
	public void initialize(Role role) {
		super.initialize(role);
		if (headIcon == null) {
			headIcon = new RoleHeadIcon();
		}
		headIcon.initialize(role);
		if (headFrame == null) {
			headFrame = new RoleHeadFrame();
		}
		headFrame.initialize(role);
		if (avartar == null) {
			avartar = new RoleAvatar();
		}
		avartar.initialize(role);
		if (title == null) {
			title = new RoleTitle();
		}
		title.initialize(role);
		if (heroSkin == null) {
			heroSkin = new RoleHeroSkin();
		}
		heroSkin.initialize(role);
		if (leaderSkin == null) {
			leaderSkin = new RoleLeaderSkin();
		}
		leaderSkin.initialize(role);
		if (bubbleBox == null) {
			bubbleBox = new RoleBubbleBox();
		}
		bubbleBox.initialize(role);
		if (vehicle == null) {
			vehicle = new RoleVehicleSkin();
		}
		vehicle.initialize(role);
	}

	@Override
	public void onEnter() {
		heroSkin.onEnter();
	}

	public void tick(){
		headIcon.tick();
		headFrame.tick();
		avartar.tick();
		title.tick();
		heroSkin.tick();
		leaderSkin.tick();
		bubbleBox.tick();
		vehicle.tick();
	}

	@Override
	public void onCreate(){
		headIcon.onCreate();
		headFrame.onCreate();
		avartar.onCreate();
	}

	public void useItem(int tid, int num) {
		role.getGame().notice.message(role, activeByTid(tid, num), true);
		role.getBase().updatePower();
	}

	public void useItem(Prop item){
		role.getGame().notice.message(role, activeByItem(item), true);
		role.getBase().updatePower();
	}

	private GameResult<Void> activeByTid(int tid, int num) {
		GameResult<Void> r = new GameResult<>();
		ActiveItemRow itemRow = role.getGame().table.blog.activeItem.get(tid);
		if (itemRow == null) {
			return r.fail("道具不存在");
		}
		while (num-- > 0) {
			GameResult<RewardShow> removeRes = role.getBag().prop.removeItem(itemRow.itemId, 1, null);
			if (!removeRes.ok()) {
				return r.fail(removeRes);
			}
			r = activeExe(itemRow);
			if (!r.ok()) {
				return r;
			}
		}
		return r.success();
	}

	private GameResult<Void> activeByItem(Prop item) {
		GameResult<Void> r = new GameResult<>();
		if (item == null) {
			return r.fail("激活道具不存在");
		}
		ActiveItemRow itemRow = role.getGame().table.blog.activeItem.get(item.tid);
		if (itemRow == null) {
			return r.fail("激活道具不存在");
		}
		GameResult<RewardShow> removeRes = role.getBag().prop.removeItem(item.uid, null);
		if (!removeRes.ok()) {
			return r.fail(removeRes);
		}
		return activeExe(itemRow);
	}


	/**
	 * 激活饰品(道具已经扣除)
	 * @param itemRow row
	 * @return r
	 */
	private GameResult<Void> activeExe(ActiveItemRow itemRow) {
		GameResult<Void> r = new GameResult<>();

		ShowBagTab bag = getBag(itemRow.decorateType);
		if (bag == null) {
			return r.fail("饰品类型不存在");
		}
		if (itemRow.decorateTids == null) {
			return r.fail("饰品道具未配置");
		}
		String noticeMsg = null;
		MixRes rwd = new MixRes();
		//主角皮肤道具会一次激活两个皮肤 不过理论上每次都是成对的
		for (Integer decorateTid : itemRow.decorateTids) {
			ShowItemBase item = bag.getItem(decorateTid);
			if (item == null) {
				item = bag.createItem(decorateTid, itemRow.valiedTime);
				bag.addItem(item, itemRow.itemId);
				noticeMsg = "激活成功";
			} else {
				GameResult<Void> extendRes = item.extendTime(itemRow.valiedTime);
				if (extendRes.ok()) {
					role.sendNow(new ActiveDecorationResp(itemRow.decorateType, item));
					noticeMsg = "延期成功";
				} else {
					if (StringUtils.isNotBlank(itemRow.backRwdStr)) {
						MixRes backRwd = new MixRes(itemRow.backRwdStr);
						if (itemRow.decorateTids.size() > 1) {
							backRwd.multiplyDouble(1d / itemRow.decorateTids.size());
						}
						rwd.addList(backRwd);
						noticeMsg = null;
					}
				}
			}
		}
		if(!rwd.isEmpty()){
			new MixResAdder().setRes(rwd).setDialogReward(true).setOperation(GDOperation.USE_PROP)
					.setOperationSub(itemRow.itemId).exe(role);
		}

		if (StringUtils.isNotBlank(noticeMsg)) {
			role.getGame().notice.message(role, noticeMsg);
		}

		return r;
	}


	private ShowBagTab getBag(byte decorateType) {
		switch (decorateType) {
			case GDBlog.DecorateType.HEAD_ICON:
				return headIcon;
			case GDBlog.DecorateType.HEAD_FRAME:
				return headFrame;
			case GDBlog.DecorateType.AVATAR:
				return avartar;
			case GDBlog.DecorateType.TITLE:
				return title;
			case GDBlog.DecorateType.HERO_SKIN:
				return heroSkin;
			case GDBlog.DecorateType.LEADER_SKIN:
				return leaderSkin;
			case GDBlog.DecorateType.BUBBLE_BOX:
				return bubbleBox;
			case GDBlog.DecorateType.VEHICLE:
				return vehicle;
			default:
				return null;
		}
	}

	public void noticeUpdate(){
		role.sendNow(new RoleShowResp(role));
	}

	@Override
	public void onDaySpan(boolean silence) {
		headIcon.onDaySpanSafe(silence);
	}
}
